/**
 * Created by zyx on 2017/9/18.
 */
angular.module('module.auth')
    .controller('ResetPwdController', ResetPwdController);

ResetPwdController.$inject = ['$state', '$stateParams', 'AuthService'];

// 重置密码
function ResetPwdController($state, $stateParams, AuthService) {
    var vm = this;
    vm.sureBtn = sureBtn; // 模态框
    vm.cancelBtn = cancelBtn; // 模态框
    vm.complete = complete;

    function sureBtn() {
        vm.messageVisible = false;
    }

    function cancelBtn() {
        vm.messageVisible = false;
    }

    var pwd1 = document.getElementById("password1");
    var pwd2 = document.getElementById("password2");
    $("#delete1").click(function () {
        pwd1.value = "";
    });
    $("#delete2").click(function () {
        pwd2.value = "";
    });

    // 获取忘记密码页面传过来的手机号和验证码
    var phone = $stateParams.phone;
    var message_code = $stateParams.code;

    function complete() {
        vm.user = {
            password: vm.password,
            phone: phone,
            code: message_code
        };
        if (pwd1.value != pwd2.value) {
            modal(true, "两次密码不一致！");
        } else {
            AuthService.resetPwd(vm.user).$promise.then(function (data) {
                if (data.state == 200) {
                    modal(true, "密码重置成功！");
                    $state.go("login")
                } else {
                    modal(true, "密码重置失败！");
                }
            })
        }
    }

    // 提示模态框控制
    function modal(status, msg) {
        vm.messageVisible = status;
        vm.message = msg;
    }
}